package comgutianyu.qihangjiaoyu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import comgutianyu.qihangjiaoyu.R;
import comgutianyu.qihangjiaoyu.VideoPlayerActivity;
import comgutianyu.qihangjiaoyu.adapter.BannerAdapter;
import comgutianyu.qihangjiaoyu.adapter.LiveClassAdapter;
import comgutianyu.qihangjiaoyu.adapter.SpecialClassAdapter;
import comgutianyu.qihangjiaoyu.model.LiveClass;
import comgutianyu.qihangjiaoyu.model.SpecialClass;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class HomeFragment extends Fragment {
    private ViewPager2 viewPager;
    private LinearLayout indicatorLayout;
    private Timer timer;
    private int currentPage = 0;
    private final int NUM_PAGES = 3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        
        viewPager = view.findViewById(R.id.viewPager);
        indicatorLayout = view.findViewById(R.id.indicatorLayout);
        
        setupViewPager();
        setupIndicators();
        startAutoScroll();
        setupRecyclerViews(view);
        
        return view;
    }

    private void setupViewPager() {
        BannerAdapter adapter = new BannerAdapter();
        viewPager.setAdapter(adapter);
        
        // 设置页面切换动画
        viewPager.setPageTransformer((page, position) -> {
            float absPos = Math.abs(position);
            page.setAlpha(1 - absPos * 0.5f);
            page.setScaleX(0.85f + 0.15f * (1 - absPos));
            page.setScaleY(0.85f + 0.15f * (1 - absPos));
        });
        
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                currentPage = position;
                updateIndicators(position % NUM_PAGES);
            }
        });
        
        viewPager.setCurrentItem(1000, false);
    }

    private void setupIndicators() {
        ImageView[] indicators = new ImageView[NUM_PAGES];
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(4, 0, 4, 0);

        for (int i = 0; i < indicators.length; i++) {
            indicators[i] = new ImageView(getContext());
            indicators[i].setImageDrawable(
                    getResources().getDrawable(i == 0 ? 
                            R.drawable.indicator_selected : 
                            R.drawable.indicator_unselected));
            indicators[i].setLayoutParams(params);
            indicatorLayout.addView(indicators[i]);
        }
    }

    private void updateIndicators(int position) {
        for (int i = 0; i < indicatorLayout.getChildCount(); i++) {
            ImageView imageView = (ImageView) indicatorLayout.getChildAt(i);
            imageView.setImageDrawable(
                    getResources().getDrawable(i == position ? 
                            R.drawable.indicator_selected : 
                            R.drawable.indicator_unselected));
        }
    }

    private void startAutoScroll() {
        timer = new Timer();
        final Handler handler = new Handler();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(() -> {
                    currentPage++;
                    viewPager.setCurrentItem(currentPage, true);
                });
            }
        }, 3000, 3000); // 3秒切换一次
    }

    private void setupRecyclerViews(View view) {
        // 设置直播课列表
        RecyclerView liveClassRecyclerView = view.findViewById(R.id.liveClassRecyclerView);
        liveClassRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        List<LiveClass> liveClasses = getLiveClassData();
        LiveClassAdapter liveAdapter = new LiveClassAdapter(liveClasses);
        
        // 设置直播课点击事件
        liveAdapter.setOnItemClickListener(liveClass -> 
            startVideoPlayer(liveClass.getTitle(), 
                "android.resource://" + requireContext().getPackageName() + "/" + R.raw.video1)
        );
        
        liveClassRecyclerView.setAdapter(liveAdapter);

        // 设置专题课列表
        RecyclerView specialClassRecyclerView = view.findViewById(R.id.specialClassRecyclerView);
        specialClassRecyclerView.setLayoutManager(
            new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        List<SpecialClass> specialClasses = getSpecialClassData();
        SpecialClassAdapter specialAdapter = new SpecialClassAdapter(specialClasses);
        
        // 设置专题课点击事件
        specialAdapter.setOnItemClickListener(specialClass -> 
            startVideoPlayer(specialClass.getTitle(), 
                "android.resource://" + requireContext().getPackageName() + "/" + R.raw.video1)
        );
        
        specialClassRecyclerView.setAdapter(specialAdapter);
    }

    // 测试数据方法
    private List<LiveClass> getLiveClassData() {
        List<LiveClass> list = new ArrayList<>();
        list.add(new LiveClass("刘财政", "12-22 19:00", R.drawable.shipinyongtu, 6952, "【计算机】25考研..."));
        list.add(new LiveClass("杨晶", "12-21 20:30", R.drawable.shipinyongtu, 6952, "【管综】25考研真..."));
        return list;
    }

    private List<SpecialClass> getSpecialClassData() {
        List<SpecialClass> list = new ArrayList<>();
        list.add(new SpecialClass("6-12月学习计划", R.drawable.shipinyongtu, 21915, 5));
        list.add(new SpecialClass("基础30讲入门课", R.drawable.shipinyongtu, 92273, 4));
        list.add(new SpecialClass("考研择校择专业", R.drawable.shipinyongtu, 13000, 2));
        return list;
    }

    // 添加新方法用于启动视频播放器
    private void startVideoPlayer(String title, String videoPath) {
        Intent intent = new Intent(getContext(), VideoPlayerActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("videoPath", videoPath);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
} 